package com.cmccgz.udacs_landlord_webapp.main.user.userinfo;


import android.content.Context;

import com.cmccgz.baselibrary.utils.NetUtils;
import com.cmccgz.baselibrary.utils.RegularUtil;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author INX
 * 修改密码presenter
 */
public class UserPwdChangedPresenter implements UserInfoContract.ChangePwdPresenter {
    private UserInfoContract.ChangePwdView view;

    public UserPwdChangedPresenter(UserInfoContract.ChangePwdView view) {
        this.view = view;
        view.setPresenter(this);
    }

    @Override
    public void changePwd(String oldPwd, String newPwd, String rePwd) {
        final Context context = GlobalContext.getInstance();
        if (StringUtils.isEmpty(oldPwd)) {
            view.changePwdError(context.getString(R.string.tips_old_pwd_empty));
            return;
        }
        if (!RegularUtil.isPassword(oldPwd)) {
            view.changePwdError(context.getString(R.string.tips_old_pwd_error));
            return;
        }
        if (StringUtils.isEmpty(newPwd)) {
            view.changePwdError(context.getString(R.string.tips_new_pwd_empty));
            return;
        }
        if (!RegularUtil.isPassword(newPwd)) {
            view.changePwdError(context.getString(R.string.tips_new_pwd_error));
            return;
        }
        if (StringUtils.isEmpty(rePwd)) {
            view.changePwdError(context.getString(R.string.tips_re_pwd_empty));
            return;
        }
        if (!newPwd.equals(rePwd)) {
            view.changePwdError(context.getString(R.string.tips_re_pwd_dissame));
            return;
        }
        if (newPwd.equals(oldPwd)) {
            view.changePwdError(context.getString(R.string.tips_old_new_same));
            return;
        }
        if (!NetUtils.isConnected(context)) {
            view.changePwdError(context.getResources().getString(R.string.noNetWork));
            return;
        }
        /*修改密码*/
        ApiService.changePassword(AppConfig.getMobile(context),oldPwd,newPwd)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.changePwdError(context.getString(R.string.tips_change_pwd_error));
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            view.changePwdSuccess();
                        }else{
                            view.changePwdError(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void start() {

    }
}
